const reqInterface = require('../../../../api/models')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isRefreshing: true, // 刷新状态
    list: [],
    searchParams: {
      page: 1,
      limit: 10,
      username: '',
      role: 0,
      status: 0
    },
    statusList: [{
      id: 0,
      name: '全部'
    }, {
      id: 1,
      name: '待审核'
    }, {
      id: 2,
      name: '审核通过'
    }, {
      id: 3,
      name: '审核不通过'
    }],
    roleList: [{
      id: 0,
      name: '全部'
    },{
      id: 1,
      name: '物业'
    }, {
      id: 2,
      name: '业主'
    }]
  },

  onShow() {
    let self = this;
    self.getList(self.data.searchParams)
  },

  getList(val) {
    let self = this;
    reqInterface.GetAccounts(val).then(res => {
      if (!res || res.length == 0) {
        wx.showToast({
          title: '没有更多数据',
          icon: 'none'
        })
      }
      self.setData({
        list: res ? self.data.list.concat(res) : self.data.list.concat([]),
        isRefreshing: false
      })
    })
  },
  onSearch(e) {
    let self = this;
    self.setData({
      list: [],
      'searchParams.username': e.detail.value
    })
    self.getList(self.data.searchParams)
  },
  // 加拉加载
  onLoadMore() {
    let self = this;
    let page = self.data.searchParams.page + 1; //获取当前页数并+1
    self.setData({
      'searchParams.page': page, //更新当前页数
    })
    self.getList(self.data.searchParams); //重新调用请求获取下一页数据
  },

  // 下拉刷新
  onRefresherPulling(e) {
    this.setData({
      isRefreshing: true
    });
  },

  onRefresherRefresh(e) {
    this.getFresh()
  },

  // 操作完毕后刷新
  getFresh() {
    let self = this;
    self.setData({
      list: [],
      searchParams: {
        page: 1,
        limit: 10,
        username: '',
        role_id: 0,
        status: 0
      },
    })
      self.getList(self.data.searchParams);
  },

  statusValue(e) {
    let self = this;
    self.setData({
      list: [],
      'searchParams.status': Number(e.detail.val)
    })
    self.getList(self.data.searchParams)
  },

  roleValue(e) {
    let self = this;
    self.setData({
      list: [],
      'searchParams.role': Number(e.detail.val)
    })
    self.getList(self.data.searchParams)
  },

  audit(e) {
    let self = this;
    wx.showModal({
      title: '是否通过审核',
      confirmText: '通过',
      cancelText: '不通过',
      success(res) {
        if (res.confirm) {
          reqInterface.PostAudit({
            id: e.currentTarget.dataset.id,
            status: 2
          }).then(res => {
            self.getFresh()
          })
        }
        if (res.cancel) {
          reqInterface.PostAudit({
            id: e.currentTarget.dataset.id,
            status: 3
          }).then(res => {
            self.getFresh()
          })
        }
      }
    })
  },

  
  del(e) {
    let self = this;
    wx.showModal({
      title: '是否要删除该数据？',
      confirmText: '删除',
      showCancel: true,
      success(res) {
        if (res.confirm) {
          reqInterface.delAccount({
            id: e.currentTarget.dataset.id
          }).then(res => {
            self.getFresh()
          })
        }
      }
    })
  }
})